<template>
	<view class="pages">
		<view
			v-if="!loading && list.length > 0"
			class="mar-x-15 pad-t-20"
		>
			<view
				v-for="(item, index) in list"
				:key="index"
			>
				<view class="font-14 font-b-800">
					<text>{{ item.SettingName }}</text>
				</view>
				
				<view class="flex flex-wrap">
					<CustomRadio
						class="mar-t-10 mar-r-10"
						v-for="(j, i) in item.ItemList"
						:groudId="item.RelationType"
						:key="i"
						:iconType="'radio'"
						:label="j.RealName"
						:name="j.UserName"
						:value="j.Checked"
						:in-active-value="1"
						:active-value="0"
						@handleRadioClick="handleRadioClick"
					/>
				</view>
				
				<Lines v-if="index !== list.length - 1" w="100%" h="1px" bg="#e6e6e6" class="mar-y-20" />
			</view>
		</view>
		
		<view v-if="list.length !== 0" class="pos-f" style="left: 15px; right: 15px; bottom: 30px;">
			<u-button type="primary" @click="saveConfig()">保 存</u-button>
		</view>
		
		<!-- 页面加载 -->
		<u-loading-page :loading="loading" loadingText="加载中..." fontSize="16px"></u-loading-page>
		
		<!-- 空数据 -->
		<u-empty v-if="list.length === 0 && !loading" mode="data" text="~暂无数据" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
	</view>
</template>

<script>
	import Lines from '@/components/Lines/index'
	import CustomRadio from '@/components/CustomRadio/index'
	import Utils from '@/utils/utils'
	
	import { mapState } from 'vuex'
	import { checkLogin } from '@/utils/lib'
	import { getDefaultConfig, saveDefaultConfig } from '@/apis/setting'
	
	export default {
		components: {
			Lines,
			CustomRadio,
		},
		
		computed: {
			...mapState({
				token: state => state.user.token
			})
		},
		
		data() {
			return {
				// 状态栏高度
				statusBarHeight: 0,
				
				// 加载
				loading: false,
				
				// 配置列表
				list: [],
			}
		},
		
		onShow() {
			checkLogin()
		},
		
		onLoad() {
			// 获取状态栏高度
			this.getStatusBarHeight()
			
			// 获取默认配置信息
			this.getInfo()
		},
		
		methods: {
			// 获取状态栏高度
			getStatusBarHeight() {
				let info = uni.getSystemInfoSync()
				
				this.statusBarHeight = info.platform === 'android' ? info.statusBarHeight : (info.statusBarHeight + 44)
			},
			
			// 获取默认配置信息
			getInfo() {
				this.loading = true
				
				getDefaultConfig(this.token).then(res => {
					this.loading = false
					this.list = res.Result
				}).catch(err => {
					this.loading = false
				})
			},
			
			// 点击 radio 组件时的回调
			handleRadioClick(name, groudId) {
				this.list.forEach(item => {
					if (item.RelationType === groudId) {
						item.ItemList.forEach(j => {
							j.Checked = 0
							
							if (j.UserName === name) {
								j.Checked = 1
							}
						})
					}
				})
			},
			
			// 保存配置
			saveConfig() {
				let data = JSON.parse(JSON.stringify(this.list))
				let list = []
				
				data.forEach(item => {
					item.pass = false
					item.pass = item.ItemList.filter(j => j.Checked === 1).length > 0 ? true : false
				})
				
				let isPass = data.filter(item => !item.pass)
				
				if (isPass.length > 0) {
					Utils.popUpMsg('none', '存在未设置默认的组！', 1000)
					return
				}
				
				// 获取参数
				data.forEach(item => {
					item.ItemList.forEach(j => {
						if (j.Checked) {
							list.push({
								RelationType: item.RelationType,
								Auditor: j.UserName
							})
						}
					})
				})
				
				let params = { list }
				
				Utils.loading('设置中...')
				
				saveDefaultConfig(params, this.token).then(res => {
					uni.hideLoading()
					Utils.popUpMsg('success', res.Message, 1000)
					this.getInfo()
				}).catch(() => {
					uni.hideLoading()
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: #fff;
	}
</style>
